﻿using System;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Windows.Input;
using System.Windows.Interactivity;
using DevExpress.Xpf.Grid;
using Loki.Utils;

namespace Loki.UI.Wpf
{
    public static class GridExtensions
    {
        static GridExtensions()
        {
            Log = Toolkit.Logger.GetLog("Loki.UI.Wpf.GridExtensions");
        }

        public static ILog Log
        {
            get;
            private set;
        }

        public static void Bind<TDisplayable>(this GridColumnBase P_Column, Expression<Func<TDisplayable, object>> P_Property)
        {
            P_Column.FieldName = ExpressionHelper.GetProperty<TDisplayable, object>(P_Property).Name;
        }

        public static void ConfigureGrid(this GridControl P_Grid)
        {
            GridExportBehaviour L_ExportBehavior = new GridExportBehaviour();
            Interaction.GetBehaviors(P_Grid).Add(L_ExportBehavior);

            TableView L_View = P_Grid.View as TableView;
            if (L_View != null)
            {
                if (L_View.AllowEditing)
                {
                    GridIsChangedBehavior L_ChangedBehaviour = new GridIsChangedBehavior();
                    Interaction.GetBehaviors(P_Grid).Add(L_ChangedBehaviour);

                    L_View.NewItemRowPosition = NewItemRowPosition.Top;

                    L_View.ValidateRow += L_View_ValidateRow;
                    P_Grid.KeyDown += P_Grid_KeyDown;
                }
                else
                {
                    L_View.NewItemRowPosition = NewItemRowPosition.None;
                }

                L_View.ShowTotalSummary = true;
            }
        }

        public static void ConfigureGrid(this TreeListControl P_Grid)
        {
            TreeListExcelExportBehaviour L_ExportBehavior = new TreeListExcelExportBehaviour();
            Interaction.GetBehaviors(P_Grid).Add(L_ExportBehavior);

            TreeListView L_View = P_Grid.View as TreeListView;
            if (L_View != null)
            {
                if (L_View.AllowEditing)
                {
                    TreeListIsChangedBehaviour L_ChangedBehaviour = new TreeListIsChangedBehaviour();
                    Interaction.GetBehaviors(P_Grid).Add(L_ChangedBehaviour);

                    /*L_View.ValidateNode += L_View_ValidateNode;

                    P_Grid.KeyDown += P_Grid_KeyDown;*/
                }

                L_View.ShowTotalSummary = true;
            }
        }

        public static void OpenOnDoubleClick<TDisplayable>(this GridControl P_Grid, Func<TDisplayable, object> P_CommandParameter)
            where TDisplayable : class
        {
            TableView L_View = P_Grid.View as TableView;
            if (L_View != null)
            {
                L_View.RowDoubleClick += (s, e) =>
                {
                    if (e.HitInfo.InColumnHeader == false
                    && e.HitInfo.InColumnPanel == false
                    && e.HitInfo.InFilterPanel == false
                    && e.HitInfo.InGroupColumn == false
                    && e.HitInfo.InGroupPanel == false
                    && e.HitInfo.InRow == true)
                    {
                        TDisplayable L_Item = P_Grid.GetRow(e.HitInfo.RowHandle) as TDisplayable;
                        Loki.Commands.ApplicationCommands.Open.Execute(P_CommandParameter(L_Item));
                    }
                };
            }
        }

        private static void L_View_ValidateNode(object sender, DevExpress.Xpf.Grid.TreeList.TreeListNodeValidationEventArgs e)
        {
            IDataErrorInfo L_Info = e.Node as IDataErrorInfo;
            if (L_Info != null)
            {
                e.IsValid = string.IsNullOrEmpty(L_Info.Error);
                e.ErrorContent = L_Info.Error;
                e.Handled = true;
            }
        }

        private static void L_View_ValidateRow(object sender, GridRowValidationEventArgs e)
        {
            IDataErrorInfo L_Info = e.Row as IDataErrorInfo;
            if (L_Info != null)
            {
                e.IsValid = string.IsNullOrEmpty(L_Info.Error);
                e.ErrorContent = L_Info.Error;
                e.Handled = true;
            }
        }

        private static void P_Grid_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            var gridControl = (GridControl)sender;

            if (e.Handled)
            {
                return;
            }

            if (e.Key == Key.Delete && !gridControl.View.IsEditing)
            {
                TableView L_View = gridControl.View as TableView;
                L_View.DeleteRow(L_View.FocusedRowHandle);

                e.Handled = true;
            }
        }
    }
}